package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduleTasks {

	// 定时任务执行注解
	// @Scheduled(cron = "* * * * * *")
	// public void test(){
	// 	System.out.println(new Date());
	// }

	@Autowired
	private LeaseAgreementService service;

	@Scheduled(cron = "0 0 0 * * *")
	public void checkLeaseStatus(){
		// 定义一个构建更新条件和设置更新字段的包装类
		LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
		// 条件为租约结束日期小于当前日期
		updateWrapper.le(LeaseAgreement::getLeaseEndDate, new Date());
		// 允许你指定某个字段的值必须在给定的一组值中
		// 租约状态必须为已签约或退租待确认
		updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
		// 修改租约状态为已到期
		updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
		// 执行更新
		service.update(updateWrapper);
	}
}
